package com.qiall.gateway.controller.websocket;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.Session;
import java.io.IOException;

@Component
@Slf4j
public class SessionManage {
    public void sendMessage(Protocol protocol) {
        Session session = SessionPool.getSession(protocol.getSessionId());
        if (session == null) {
            log.error("session is null session id is [{}]", protocol.getSessionId());
        }
        try {
            session.getBasicRemote().sendText(JSON.toJSONString(protocol));
        } catch (IOException e) {
            e.printStackTrace();
            log.error("send message fail session id is [{}] ,error message is [{}]",protocol.getSessionId(),e.getMessage());
        }
    }
}
